package wh.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wh.crm.dao.CustomerRepository;
import wh.crm.domain.Customer;
import wh.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{

	@Autowired
	private CustomerRepository customerRepository;
	@Override
	public List<Customer> findNoAssociationCustomers() {
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findAssociationFixedAreaCustomers(String fixedAreaId) {
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void associationCustomerToFixedArea(String customerIdStr, String fixedAreaId) {
			customerRepository.clearFixAreaId(fixedAreaId);
			if (null != customerIdStr && !customerIdStr.equals("") && !customerIdStr.equals("null")) {
				String[] result = customerIdStr.split(",");
				for (String string : result) {
					int id = Integer.parseInt(string);
					customerRepository.updateFixedAreaId(fixedAreaId,id);
			}
		}
	}

	@Override
	public void regist(Customer customer) {
		customerRepository.save(customer);
	}

	@Override
	public Customer findByTelephone(String telephone) {
		return customerRepository.findByTelephone(telephone);
	}

	@Override
	public void updateType(String telephone) {
		customerRepository.updateType(telephone);
	}

	@Override
	public Customer login(String telephone, String password) {
		return customerRepository.findByTelephoneAndPassword(telephone,password);
	}

	@Override
	public String findFixedAreaIdByCustomerId(Integer customer_id) {
		return customerRepository.findFixedAreaIdByCustomerId(customer_id);
	}


}
